begintownscript;

variables;
short choice;
short augment;
short laser1 = 0;

body;

beginstate 0; //INIT_STATE

	//force the party into a reasonable starting area
	relocate_character(0,21,27);
	relocate_character(1,20,27);
	relocate_character(2,19,27);
	relocate_character(3,19,26);
	relocate_character(4,20,26);
	force_view_center(21,27);
	force_instant_terrain_redraw();
	
	//groups
	add_range_to_group(8,18,1); //shrubs!
	add_char_to_group(22,1);
	set_attitude(1001,4);

	//intro dialog
	if(get_flag(11,0) == 0) {
		force_instant_terrain_redraw();
		pause(5);
		
		reset_dialog();
		add_dialog_str(0,"You descend into the tunnels, and are nearly overwhelmed by the scent of stale air. If there's actually a way out of the city down here, it's clearly not helping the air circulation.",0);
		add_dialog_str(1,"The tunnels themselves appear to be carved out of the dirt by magic. It's far too regular to be a normal excavation, and just haphazard enough to look like a wizard's side-project.",0);
		add_dialog_str(2,"In any event, you hope that the experiments housed down here aren't dangerous.",0);
		add_dialog_str(3,"Of course, they probably are. Why else would they be down here in the first place?",0);
		run_dialog(1);
		
		set_flag(11,0,1);
		}

break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE

	//if the laser has mellowed, blow up the passage out!
	if(laser1 == 2) {
		play_sound(-108);
		pause(5);
		play_sound(-108);
		pause(3);
		play_sound(-108);
		pause(4);
		play_sound(-108);
		pause(6);
		
		//now, the big terrain overhaul
		set_terrain(36,4,137);
		set_terrain(36,3,137);
		set_terrain(36,2,137);
		set_terrain(36,1,137);
		set_terrain(35,3,41);
		set_terrain(35,2,41);
		set_terrain(35,1,41);
		set_terrain(37,3,39);
		set_terrain(37,2,39);
		set_terrain(37,1,39);
		set_floor(36,3,72);
		set_floor(36,2,72);
		set_floor(36,1,72);
		force_instant_terrain_redraw();
		
		message_dialog("With a thundering rumble, a cloud of debris blasts out of the tunnel to the north. A quick glance reveals a mostly-clear path.","");
		
		laser1 = 0;
		set_flag(11,16,1);
		}
	//check the laser, give it one turn to mellow
	if(laser1 == 1)
		laser1 = 2;

	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
break;

beginstate 10; //ladder back up
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"There's a ladder here, with a trapdoor above it. Do you ascend?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if(choice == 2)
		move_to_new_town(6,11,28);
break;

beginstate 15; //ozone
	if(get_flag(11,1) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"There's a faint smell of ozone back here. That might account for the collapsed tunnel.",0);
	add_dialog_str(1,"Hopefully this wasn't the way out at some point.",0);
	run_dialog(1);
	set_flag(11,1,1);
break;

beginstate 20; //wolf den
	if(get_flag(11,2) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	if(get_flag(11,8) == 0)
		add_dialog_str(0,"You can't help but wonder what kind of experiment was carried out in this room. Something was clearly living here, and it ate meat.",0);
	else
		add_dialog_str(1,"You have a bad feeling that you know what used to live in this room. As for what kind of experiment required it, you'd rather not think too much about it.",0);
	run_dialog(1);
	set_flag(11,2,1);
break;

beginstate 21; //searching the wolf nest
	if(get_flag(11,3) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	if(get_flag(11,8) == 0)
		add_dialog_str(0,"You rummage around in the nest, hoping to find some clue as to what lived down here, and you find what appears to be a bloody human finger.",0);
	else
		add_dialog_str(0,"You rummage around in the nest, hoping that none of the crazed wolves sneak up on you in the process. You find, among other trinkets, what appears to be a bloody human finger.",0);
	add_dialog_str(1,"Disgusted, you toss it away. You haven't seen anyone with less than ten fingers lately, so you don't know whose it is.",0);
	run_dialog(1);
	set_flag(11,3,1);
break;

beginstate 25; //summoning chamber
	if(get_flag(11,4) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"The purpose of this chamber seems relatively clear. It was a summoning chamber of some kind. Thankfully, no monsters seem to have been left behind.",0);
	run_dialog(1);
	set_flag(11,4,1);
break;

beginstate 26; //don't even let them jump into the pit
	block_entry(1);
break;

beginstate 30; //foreboding growl
	if(get_flag(11,5) == 1)
		end();
	print_str("You hear a long, low growl from up ahead.");
	set_flag(11,5,1);
break;

beginstate 35; //supply chamber
	if(get_flag(11,6) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"A few dusty cabinets and a disused mop suggest that this is the catch-all supply closet for the tunnels.",0);
	run_dialog(1);
	set_flag(11,6,1);
break;

beginstate 40; //augmentation chamber
	if(get_flag(11,7) == 1)
		end();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"This room is mostly bare, aside from a long table, a strange platform, and a lever.",0);
	if(get_flag(11,8) == 0)
		add_dialog_str(1,"Then you hear a growl, and look to the corner of the room. A giant blue wolf with distorted features stands over a chunk of raw meat, staring straight through you with soulless black eyes.",0);
	else
		add_dialog_str(1,"Then you hear a growl, and look to the corner of the room. Another of the bizarre wolves stands over a chunk of raw meat, staring straight through you.",0);
	if(get_flag(11,2) == 1)
		add_dialog_str(2,"All of a sudden you have a bad feeling that you know what the other experiment was raising.",0);
	run_dialog(1);
	set_flag(11,7,1);
	//if you haven't seen a wolf, you have now
	if(get_flag(11,8) == 0)
		set_flag(11,8,1);
break;

beginstate 41; //dead end
	if(get_flag(11,9) == 1)
		end();
	force_instant_terrain_redraw();
	pause(3);
	message_dialog("Just when you were getting your hopes up, this short passage dead-ends in a rockfall. If this leads out of the city, you won't be using it any time soon.","");
	set_flag(11,9,1);
break;

beginstate 42; //pull the lever! (specobj trigger)
	reset_dialog_preset_options(2);
	choice = run_dialog(1);
	if(choice == 1)
		end();
	else {
		if(get_flag(11,13) == 1) {
			play_sound(-94); //lever clunk
			message_dialog("You pull the lever. Nothing happens.","");
			flip_terrain(30,21);
			force_instant_terrain_redraw();
			end();
			}
		else {
			augment = 0;
			//if there's an item on the platform, note it
			if(item_of_class_on_spot(28,22,-1) == 1)
				augment = 1;
			play_sound(-94); //lever clunk
			flip_terrain(28,22);
			force_instant_terrain_redraw();
			pause(3);
			play_sound(65); //draining sound
			pause(1);
			put_boom_on_space(28,22,4,0);
			run_animation_sound(54); //lightning
			move_item_on_spot(28,22,-1,-1); //whoops, items are destroyed
			damage_near_loc(28,22,30,2,3); //30 magic damage within 2
			reset_dialog();
			add_dialog_str(0,"You pull the lever, and a pulse of bright light fills the room. The sudden outpouring of magic burns your skin and hurts your eyes.",0);
			if(augment == 1)
				add_dialog_str(1,"When the afterimage clears from your vision, you see that the platform is empty, aside from a bit of charred residue. Damn.",0);
			run_dialog(1);
			set_flag(11,13,1);
			}
		}
break;

beginstate 45; //dead end
	if(get_flag(11,10) == 1)
		end();
	force_instant_terrain_redraw();
	pause(3);
	message_dialog("Though this passage wandered a bit farther from the experiments, it abruptly dead-ends at a large rockfall. If this leads out of the city, you won't be using it any time soon.","");
	set_flag(11,10,1);
break;

beginstate 50; //wolf ambush
	if(get_flag(11,11) == 1)
		end();
	force_instant_terrain_redraw();
	pause(3);
	reset_dialog();
	add_dialog_str(0,"Strange, this passage splits off into a much rougher-looking tunnel, lined with debris. You're mulling over what could've caused it when you hear a loud growl.",0);
	if(get_flag(11,8) == 0)
		add_dialog_str(1,"You look over to see a giant blue wolf with distorted features, pawing a set of bones and staring at you with soulless black eyes.",0);
	else
		add_dialog_str(1,"You look over to see another one of the bizarre wolves, pawing at a set of bones and staring at you.",0);
	run_dialog(1);
	set_flag(11,11,1);
	//if you haven't seen a wolf, you have now
	if(get_flag(11,8) == 0)
		set_flag(11,8,1);
break;

beginstate 60; //the shrubbery room
	if(get_flag(11,12) == 1)
		end();
	force_instant_terrain_redraw();
	pause(3);
	reset_dialog();
	add_dialog_str(0,"You enter into a larger chamber, and you're almost certain that this is where Tyler conducts his experiments.",0);
	//if anyone has Nature Lore over 6
	if(get_highest_skill(16) > 6)
		add_dialog_str(1,"The room is dotted with ominous-looking plants that seem to glow with a strange inner light. They remind you of a variety of shrub that produces magical fire in its growths.",0);
	//otherwise, if anyone has Arcane Lore over 8
	else if(get_highest_skill(13) > 8)
		add_dialog_str(1,"The room is dotted with strange-looking plants that seem to glow with a strange inner light. You've heard of such plants, but don't know anything about them aside from the fact that they're magical.",0);
	//otherwise, no dice
		add_dialog_str(1,"The room is dotted with strange-looking plants that seem to glow. At least, you think they are plants, because they don't look like anything you've ever seen before.",0);
	add_dialog_str(2,"At the edges of the room, you can see some strange-looking machinery. More importantly, you think you see a gap in the far wall.",0);
	add_dialog_str(3,"Then a few sharp growls remind you that you're not alone.",0);
	run_dialog(1);
	set_attitude(1001,10); //make the shrubs hostile
	set_flag(11,12,1);
break;

beginstate 61; //dead end?
	if((get_flag(11,14) == 1) || (get_flag(11,16) == 1))
		end();
	force_instant_terrain_redraw();
	pause(3);
	message_dialog("This passage catches your eye, because the debris that blocks it looks a bit looser. And unless you're hallucinating, you think you can feel a thin breeze coming from it.","There's more debris than you can clear yourself, but maybe you can find a way to blast it away.");
	set_flag(11,14,1);
break;

beginstate 62; //control panel (specobj trigger)
	if(get_flag(11,15) == 2) {
		message_dialog("The control panel is dark and unresponsive.","");
		end();
		}
	reset_dialog_preset_options(4); //button dialog
	choice = run_dialog(1);
	if(choice == 2) {
		//if the laser's already on
		if(get_flag(11,15) == 1) {
			//if you just pressed it this turn
			if(laser1 > 0) {
				play_sound(37); //button
				message_dialog("You press the button, and nothing happens. Maybe if you waited a second, it would work properly.","");
				end();
				}
			play_sound(37); //button
			pause(5);
			play_sound(-61); //summoning boing
			set_terrain(37,12,388); //turn off the laser
			force_instant_terrain_redraw();		
			pause(5);
			reset_dialog();
			add_dialog_str(0,"The orb goes dark, and you see the beam begin to dim.",0);
			add_dialog_str(1,"Thankfully, nothing explodes. The orb simply collapses in on itself with a loud tinkle of glass.",0);
			run_dialog(1);	
			pause(3);
			play_sound(-108); //crumbling rock
			set_terrain(37,12,385); //break the power source
			force_instant_terrain_redraw();
			set_flag(11,15,2);
			end();
			}
		//if you're turning it on the first time
		else {
			laser1 = 1;
			play_sound(37); //button
			pause(5);
			play_sound(-61); //summoning boing
			set_terrain(37,12,384); //turn on the laser
			force_instant_terrain_redraw();
			pause(5);
			reset_dialog();
			add_dialog_str(0,"The raised orb to the south begins to glow alarmingly. You have a sinking feeling about this...",0);
			run_dialog(1);
			set_flag(11,15,1);
			end();
			}
		}
break;

beginstate 63; //the exit!
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"Ahead lies a tunnel, and hopefully it leads out from under Copperpeak. Do you follow it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if(choice == 1)
		end();
	else
		move_to_new_town(12,24,22);
break;